#include "WindowTitleBar.h"
#include "ui_WindowTitleBar.h"
#include <QPainter>
#include <QPen>
#include <QMouseEvent>
#include <QDebug>

WindowTitleBar::WindowTitleBar(QWidget *parent) :
	TitleBarAbstract(parent),
	ui(new Ui::WindowTitleBar)
{
	ui->setupUi(this);
	// image generated using   http://cooltext.com/Logo-Design-Alien-Glow
	logoImage.setPixmap(QPixmap(":/logo.png"));
	logoImage.setParent(this);

	connect(ui->btnClose,SIGNAL(clicked()),SIGNAL(close()));
	connect(ui->btnMaximize,SIGNAL(clicked()),SLOT(onBtnMaximize()));
	connect(ui->btnMinimize,SIGNAL(clicked()),SIGNAL(minimize()));

	ui->btnMaximize->setProperty("state",QVariant(false));

	ui->btnClose->setStyleSheet(getButtonStyle("close"));
	ui->btnMaximize->setStyleSheet(getButtonStyle("max"));
	ui->btnMinimize->setStyleSheet(getButtonStyle("min"));

	logoImage.setMouseTracking(true);
}

QString WindowTitleBar::getButtonStyle(QString btn){
	QString str = "QPushButton{background:url(:/buttons/BTN_normal.png);border:0px;}"
			"QPushButton:hover{background:url(:/buttons/BTN_hover.png);border:0px;}"
			"QPushButton:pressed{background:url(:/buttons/BTN_pressed.png);border:0px;}";
	return str.replace("BTN",btn);
}

WindowTitleBar::~WindowTitleBar()
{
	delete ui;
}

void WindowTitleBar::onBtnMaximize()
{
	bool state = ui->btnMaximize->property("state").toBool();
	if(!state){ // maximize
		emit maximize();
	}
	else{ // normalize
		emit normalize();
	}
	// change current state
	ui->btnMaximize->setProperty("state",QVariant(!state));
}

void WindowTitleBar::paintHeader(QPainter* painter)
{
	int radius = 8;
	QPainterPath first;
	first.moveTo(0,height());
	first.lineTo(0,radius-1);
	first.arcTo(0,0,2*radius,2*radius,180,-90);
	first.lineTo(width()-radius-1,0);

	QPainterPath second;
	second.moveTo(width()-radius-1,0);
	second.arcTo(width()-2*radius-1,0,2*radius,2*radius,90,-90);
	second.lineTo(width()-1,height());

	QPainterPath path = first + second;
	path.lineTo(0,height());
	QLinearGradient linearGrad(QPointF(0, 0), QPointF(0, height()));

	linearGrad.setColorAt(0, QColor(153,180,209));
	linearGrad.setColorAt(1, QColor(184,208,233));

	painter->setPen(QColor(52,52,52));
	painter->setBrush(QBrush(linearGrad));

	painter->fillPath(path,QBrush(linearGrad));
	painter->drawPath(path);

	painter->setPen(QColor(255,255,255));
	first.translate(QPoint(1,1));
	painter->drawPath(first);

	painter->setPen(QColor(105,217,243));
	second.translate(QPoint(-1,1));
	painter->drawPath(second);

	logoImage.setGeometry(width()/2 - 158/2,2,158,32);
}
